<?php

class m_merk extends CI_Model {

    function __construct() {
        // Call the Model constructor
        parent::__construct();
    }

    function get_count_all_merk() {
        $sql = "SELECT COUNT(merk_cd)'total' FROM merk";
        $query = $this->db->query($sql);
        if ($query->num_rows() > 0) {
            $result = $query->row_array();
            $query->free_result();
            return $result['total'];
        } else {
            return array();
        }
    } 
    
     function get_count_all_merk_search($params) {
        $sql = "SELECT COUNT(merk_cd)'total' FROM merk WHERE merk_name LIKE ? AND kategoribarang_cd LIKE ?";
        $query = $this->db->query($sql, $params);
        if ($query->num_rows() > 0) {
            $result = $query->row_array();
            $query->free_result();
            return $result['total'];
        } else {
            return array();
        }
    } 
    // check 
    function is_exist_merk($params) {
        $sql = "SELECT * FROM merk
                WHERE merk_cd = ? AND kategoribarang_cd = ?";
        $query = $this->db->query($sql, $params);
        if ($query->num_rows() > 0) {
            $query->free_result();
            return true;
        } else {
            return false;
        }
    }

    //cek data
    function get_count_merk_by_kategoribarang_cd($params) {
        $sql = "SELECT COUNT(*) 'total' FROM merk WHERE kategoribarang_cd = ?";
        $query = $this->db->query($sql, $params);
        if ($query->num_rows() > 0) {
            $result = $query->row_array();
            $query->free_result();
            return $result['total'];
        } else {
            return array();
        }
    }

    // get user profil
    function get_all_merk() {
        $sql = "SELECT a.* FROM merk a
                ORDER BY a.merk_name ASC";
        $query = $this->db->query($sql);
        if ($query->num_rows() > 0) {
            $result = $query->result_array();
            $query->free_result();
            return $result;
        } else {
            return array();
        }
    }

    function get_merk_by_kategoribarang_cd($params) {
        $sql = "SELECT * FROM merk WHERE kategoribarang_cd = ? ORDER BY CAST(merk_cd AS UNSIGNED) ASC";
        $query = $this->db->query($sql, $params);
        if ($query->num_rows() > 0) {
            $result = $query->result_array();
            $query->free_result();
            return $result;
        } else {
            return array();
        }
    }

    function get_all_merk_pagination($params) {
        $sql = "SELECT a.*, b.kategoribarang_name FROM merk a
                JOIN kategoribarang b ON a.kategoribarang_cd =b.kategoribarang_cd
                ORDER BY b.kategoribarang_name, CAST(a.merk_cd AS UNSIGNED) ASC
                LIMIT ?, ?";
        $query = $this->db->query($sql, $params);
        if ($query->num_rows() > 0) {
            $result = $query->result_array();
            $query->free_result();
            return $result;
        } else {
            return array();
        }
    }

    function get_all_merk_pagination_search($params) {
        $sql = "SELECT a.*, b.kategoribarang_name FROM merk a
                JOIN kategoribarang b ON a.kategoribarang_cd =b.kategoribarang_cd
                WHERE a.merk_name LIKE ? AND a.kategoribarang_cd LIKE ?
                ORDER BY b.kategoribarang_name, CAST(a.merk_cd AS UNSIGNED) ASC
                LIMIT ?, ?";
        $query = $this->db->query($sql, $params);
        if ($query->num_rows() > 0) {
            $result = $query->result_array();
            $query->free_result();
            return $result;
        } else {
            return array();
        }
    }
    function get_merk_by_id($params) {
        $sql = "SELECT a.*, b.kategoribarang_name FROM merk a
                JOIN kategoribarang b ON a.kategoribarang_cd =b.kategoribarang_cd
                WHERE a.merk_cd = ?";
        $query = $this->db->query($sql, $params);
        if ($query->num_rows() > 0) {
            $result = $query->row_array();
            $query->free_result();
            return $result;
        } else {
            return array();
        }
    }
    
     function get_merk_by_kategoribarang_cd_merk_cd($params) {
        $sql = "SELECT a.*, b.kategoribarang_name FROM merk a
                JOIN kategoribarang b ON a.kategoribarang_cd =b.kategoribarang_cd
                WHERE a.kategoribarang_cd = ? AND a.merk_cd = ?";
        $query = $this->db->query($sql, $params);
        if ($query->num_rows() > 0) {
            $result = $query->row_array();
            $query->free_result();
            return $result;
        } else {
            return array();
        }
    }
    
    function get_merk_by_kategoribarang($params) {
        $sql = "SELECT a.*, b.kategoribarang_name FROM merk a
                JOIN kategoribarang b ON a.kategoribarang_cd =b.kategoribarang_cd
                WHERE a.kategoribarang_cd = ? ORDER BY CAST(merk_cd AS UNSIGNED) ASC";
        $query = $this->db->query($sql, $params);
        if ($query->num_rows() > 0) {
            $result = $query->result_array();
            $query->free_result();
            return $result;
        } else {
            return array();
        }
    }

    // insert
    function insert($user_id) {
        // update by this date
        $sql = "INSERT INTO merk VALUES (?, ?, ?, ?, NOW())";
        return $this->db->query($sql, $user_id);
    }

    // edit
    function edit($user_id) {
        // update by this date
        $sql = "UPDATE merk SET merk_name = ?,mdb = ? WHERE kategoribarang_cd = ? AND merk_cd = ?";
        return $this->db->query($sql, $user_id);
    }

    // delete
    function delete($user_id) {
        // update by this date
        $sql = "DELETE FROM merk WHERE kategoribarang_cd = ? AND merk_cd = ?";
        return $this->db->query($sql, $user_id);
    }
    
    function get_barang_by_id($params) {
        $sql = "SELECT * FROM merk WHERE merk_cd = ? AND kategoribarang_cd = ?";
        $query = $this->db->query($sql, $params);
        if ($query->num_rows() > 0) {
            $result = $query->row_array();
            $query->free_result();
            return $result;
        } else {
            return array();
        }
    }

}
